/**
 * Created by yunpeng on 2015/4/13.
 */
$('#userdg').datagrid({
    url:'/admin/usersList',
    //border : false,
    rownumbers: true,
    idField : 'id',
    pagination : true,
    sortName : 'createtime',//默认排序的列
    sortOrder: 'desc',
    pageSize : 10,
    pageList : [ 10, 20, 40, 80 ],
    fitColumns : true,
    fit : true,
    checkOnSelect : false,
    selectOnCheck : true,
    striped: true,//条纹交替
    columns:[[
        {field:'',checkbox:true},
        /*{field:'lid',title:'序号',width:50},*/
        {field:'phone',title:'手机',width:200,align:'center'},
        {field:'nickname',title:'昵称',width:150,align:'center'},
        {field:'name',title:'姓名',width:150,align:'center'},
        {field:'idcard',title:'身份证号',width:250,align:'center'},
       /* {field:'idCardPath',title:'身份证',width:100,align:'center'},*/
        {field:'usertype',title:'用户类型',width:100,align:'center',formatter : function(value, rowData, rowIndex) {
            if(value==1){
                return '<span style="color: #24748f">发车方</span>';
            }else if(value==2){
                return '承运方';
            }

        }},
        {field:'createtime',title:'注册时间',width:200,align:'center'},
        {field:'authstate',title:'发车认证',width:70,align:'center',formatter : function(value, rowData, rowIndex) {
            if(value==0){
                return '未认证';
            }else if(value==1){
                return '已认证';
            }else{
                return '<span style="color: #ff0000">待审核</span>';
            }

        }},
        {field:'carrierauthstate',title:'承运认证',width:70,align:'center',formatter : function(value, rowData, rowIndex) {
            if(value==0){
                return '未认证';
            }else if(value==1){
                return '已认证';
            }else{
                return '<span style="color: #ff0000">待审核</span>';
            }

        }},
        {field:'address',title:'地址',width:200,align:'center'
            /*formatter : function(value, rowData, rowIndex) {
             //return rowData.flgcxxbqxj.tbrq;
             var unixTimestamp = new Date(new Date(value).getDate().format("yyyy-MM-dd hh:mm:ss");///格式化时间到小时
             return unixTimestamp;

             }*/},
        {field:'introduce',title:'自我介绍',width:200,align:'center'},
        {field:'company',title:'公司',width:200,align:'center'}/*,
        {field:'licences',title:'营业执照',width:200,align:'center'},
        {field:'roadlicences',title:'道路许可证',width:200,align:'center'},
        {field:'evaluationnum',title:'评分',width:200,align:'center'},
        {field:'attitude',title:'服务态度',width:200,align:'center'},
        {field:'care',title:'关心货物',width:200,align:'center'},
        {field:'corresponds',title:'符合程度',width:200,align:'center'},
        {field:'headerpath',title:'头像',width:200,align:'center'}*/

    ]],
    toolbar : '#tb'
});
/*$('#usertype').combobox('select','2');*/
/*$('#usertype').combobox({panelHeight:"70px"});*/


function searchUser(){
    //alert($('#usertype').combobox('getValue'))
    $('#userdg').datagrid('load',{
        date1: $('#date1').datebox('getValue'),
        date2: $('#date2').datebox('getValue'),
        phone: $('#phone').textbox('getValue'),
        type: $('#usertype').combobox('getValue')
    });
}


function userDetail(){//编辑商家信息
    var rows = $('#userdg').datagrid('getChecked');
    if (rows.length == 1) {
        //动态加载
        var detailWindow = $('<div/>')
            .dialog(
            {
                width : 640,
                height : 620,
                href : '/admin/views/userdetail.html',
                modal : true,
                title : '详细信息',
                maximizable:true,
                cache:false,
                minimized:true,
                buttons : [{
                    text : '取&nbsp;&nbsp;消',
                    width : 60,
                    handler : function() {
                        detailWindow.dialog('close');
                    }
                } ],
                onClose : function() {
                    $('#userdg').datagrid('uncheckAll');// 取消选中的所有记录
                    $(this).dialog('destroy');//动态加载一定要销毁
                },
                //加载数据，用onLoad，不是用onOpen
                onLoad : function() {
                    if(rows[0].authstate==0){
                        rows[0].authstate = '未认证'
                    }else if(rows[0].authstate==1){
                        rows[0].authstate = '已认证'
                    }else if(rows[0].authstate==2){
                        rows[0].authstate = '等待认证';
                    }
                    if(rows[0].carrierauthstate==0){
                        rows[0].carrierauthstate = '未认证'
                    }else if(rows[0].carrierauthstate==1){
                        rows[0].carrierauthstate = '已认证'
                    }else if(rows[0].carrierauthstate==2){
                        rows[0].carrierauthstate = '等待认证';
                    }
                    $('#userdetail').form('load', rows[0]);

                    if(rows[0].idcard){
                        $('#idcard').attr("src","/image/userimg/userid/"+rows[0].idCardPath);//'bianfuxia.png'
                    }
                    //alert(rows[0].licences+' '+rows[0].roadlicences);
                    if(rows[0].licences){
                        $('#licences').attr("src","/image/userimg/userid/"+rows[0].licences);
                    }
                    if(rows[0].roadlicences){
                        $('#roadlicences').attr("src","/image/userimg/userid/"+rows[0].roadlicences);
                    }
                }
            });
    } else {
        $.messager.alert('提示', '请选择1条记录', 'error');
    }
}
function audit(num){
    var rows = $('#userdg').datagrid('getChecked');
    if(num==1){
        if (rows.length == 1) {
            //动态加载
            if(rows[0].authstate==0) {
                $.messager.alert('提示', '此用户还未提交认证', 'error');
            }else if(rows[0].authstate==1){
                $.messager.alert('提示', '此用户已经通过认证', 'error');
            }else{
                var detailWindow = $('<div/>')
                    .dialog(
                    {
                        width : 340,
                        height : 220,
                        href : '/admin/views/audit.html',
                        modal : true,
                        title : '详细信息',
                        maximizable:true,
                        cache:false,
                        minimized:true,
                        buttons : [
                            {
                                text : '确&nbsp;&nbsp;定',
                                width : 60,
                                handler : function() {
                                    var type= $('#audittype').combobox('getValue');
                                    var phone=rows[0].phone;
                                    var id=rows[0].id;
                                    if(type==0){
                                        type=1;
                                    }else{
                                        type=0;
                                    }
                                    var _json = jQuery.param({ "type": type, "id": id,"phone":phone,"authstate":"authstate"});
                                    var request = $.ajax({
                                        url: "/admin/audit",
                                        type: "POST",
                                        async: false,
                                        data: _json, //不能直接写成 {id:"123",code:"tomcat"}
                                        dataType: "json",
                                        // contentType: "charset=utf-8",
                                        cache: false,
                                        success: function (data) {
                                            $('#userdg').datagrid('reload');
                                            console.log(data);
                                        },
                                        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }
                                    });
                                    detailWindow.dialog('close');
                                }
                            },
                            {
                                text : '取&nbsp;&nbsp;消',
                                width : 60,
                                handler : function() {
                                    detailWindow.dialog('close');
                                }}
                        ],
                        onClose : function() {
                            $('#userdg').datagrid('uncheckAll');// 取消选中的所有记录
                            $(this).dialog('destroy');//动态加载一定要销毁
                        },
                        //加载数据，用onLoad，不是用onOpen
                        onLoad : function() {

                        }
                    });
            }
        } else {
            $.messager.alert('提示', '请选择1条记录', 'error');
        }
    }else if(num==2){
        if (rows.length == 1) {
            //动态加载
            if(rows[0].carrierauthstate==0){
                $.messager.alert('提示', '此用户还未提交认证', 'error');
            }else if(rows[0].carrierauthstate==1){
                $.messager.alert('提示', '此用户已经通过认证', 'error');
            }else{
                var detailWindow = $('<div/>')
                    .dialog(
                    {
                        width : 340,
                        height : 220,
                        href : '/admin/views/audit.html',
                        modal : true,
                        title : '详细信息',
                        maximizable:true,
                        cache:false,
                        minimized:true,
                        buttons : [
                            {
                                text : '确&nbsp;&nbsp;定',
                                width : 60,
                                handler : function() {
                                    var type= $('#audittype').combobox('getValue');

                                    var phone=rows[0].phone;
                                    var id=rows[0].id;
                                    if(type==0){
                                        type=1;
                                    }else{
                                        type=0;
                                    }
                                    var _json = jQuery.param({ "type": type, "id": id,"phone":phone,"authstate":"carrierauthstate"});
                                    var request = $.ajax({
                                        url: "/admin/audit",
                                        type: "POST",
                                        async: false,
                                        data: _json, //不能直接写成 {id:"123",code:"tomcat"}
                                        dataType: "json",
                                        // contentType: "charset=utf-8",
                                        cache: false,
                                        success: function (data) {
                                            $('#userdg').datagrid('reload');
                                            console.log(data);
                                        },
                                        error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.readyState); }
                                    });
                                    detailWindow.dialog('close');
                                }
                            },
                            {
                                text : '取&nbsp;&nbsp;消',
                                width : 60,
                                handler : function() {
                                    detailWindow.dialog('close');
                                }}
                        ],
                        onClose : function() {
                            $('#userdg').datagrid('uncheckAll');// 取消选中的所有记录
                            $(this).dialog('destroy');//动态加载一定要销毁
                        },
                        //加载数据，用onLoad，不是用onOpen
                        onLoad : function() {

                        }
                    });
            }
        } else {
            $.messager.alert('提示', '请选择1条记录', 'error');
        }
    }
}